import myApi from './myapi.js'
module.exports = (params) => {
	let url = params.url;
	let method = params.method;
	let header = params.header || {};
	let data = params.data || {};
	//	请求方式 GET POST
	// console.log("发送数据-------------********************************", JSON.stringify(data));
	if (method) {
		method = method.toUpperCase(); //	小写转大写
		if (method == "POST") {
			header = {
				"content-type": "application/x-www-form-urlencoded"
			}
		}
	}
	//	发起请求 加载动画
	if (!params.hideLoading) {
		uni.showLoading({
			title: "加载中",
			mask: true
		})

	}

	try {
		const value = uni.getStorageSync('token') || 1;
		const Clientid = uni.getStorageSync('clientID') || 1;

		header = {
			"content-type": "application/json;charset=UTF-8",
			"Authorization": value,
			Clientid: Clientid
		}

	} catch (e) {
		// error
	}
	
	let _this = this;
	//	发起网络请求
	uni.request({
		url: url,
		method: method || "GET",
		header: header,
		data: data,
		dataType: "json",
		sslVerify: false, //	是否验证ssl证书
		success: res => {
			
			
			if (res.statusCode !== 200 && res.data.code != 200) {
				//	api错误
				// uni.showModal({
				// 	content:res.data.msg
				// })
				
				if (res.data.code == 303) {
					uni.showToast({
						title: res.data.message,
						duration: 2000,
						icon: 'none'
					});
					return;
				} else if (res.statusCode == 500) {
					uni.showToast({
						title: '500-服务器内部错误',
						duration: 2000,
						icon: 'none'
					});
					return;
				} else if (res.statusCode == 404) {
					uni.showToast({
						title: '404-所请求的页面不存在',
						duration: 2000,
						icon: 'none'
					});
					return;
				} else if (res.data.code == 403) {
					let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
					let curRoute = routes[routes.length - 1].route //获取当前页面路由
					let curParam = routes[routes.length - 1].options; //获取路由参数
					console.log('当前路由---------', curRoute)
					console.log('路由参数---------', curParam)
					console.log(123)
					if (curRoute != 'pages/Login/index') {
						try {
							uni.setStorageSync('PrevUrl', curRoute);
						} catch (e) {
							// error
						}
						uni.redirectTo({
							url: '/pages/Login/index?InvitedCode=' + curParam.userId
						});

					}
					return;
				} else if (res.data.code == 401) {
					// let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
					// let curRoute = routes[routes.length - 1].route //获取当前页面路由
					// let curParam = routes[routes.length - 1].options; //获取路由参数
					// console.log('当前路由---------', curRoute)
					// console.log('当前路由参数---------', curParam)
					// if (curRoute != 'pages/Login/index') {
					// 	try {
					// 		uni.setStorageSync('PrevUrl', curRoute);
					// 	} catch (e) {
					// 		// error
					// 	}
					// 	uni.redirectTo({
					// 		url: '/pages/Login/index?InvitedCode=' + curParam.userId
					// 	});
					// 	params.success(res.data);
					// }
					// return;
				}
				console.log(res.code, 2222222222222222222222222222222222222)
				return;
			}
			
			if(res.data) {
				
				// console.log(res.data, '获取了什么数据')
				if(res.code == 401) {
					console.log('登录不同的时候')
					let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
					let curRoute = routes[routes.length - 1].route //获取当前页面路由
					let curParam = routes[routes.length - 1].options; //获取路由参数
					console.log('当前路由---------', curRoute)
					console.log('当前路由参数---------', curParam)
					uni.removeStorageSync('token');
					uni.removeStorageSync('clientID')
					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							console.log(loginRes)
							// 在这里处理登录成功后的逻辑，如获取用户信息等  
							uni.request({
								url: myApi.url + '/auth/xcx-login',
								method: 'POST',
								data: {
									"xcxCode": loginRes.code,
									"clientId": "25e5f3649dded6a3345ef8ee6359cda0",
									"grantType": "xcx",
									"appid": "wx767edd216cfa44e2"
								},
								success: res => {
									uni.removeStorageSync('openId');
									// 存取token
									uni.setStorageSync('token', 'Bearer ' + res.data.access_token)
									uni.setStorageSync('clientID', res.data.client_id)
									uni.setStorageSync('openId', res.data)
									// 路由跳转
									console.log('可以路由跳转了')
									try {
										uni.reload();
										console.log('执行了？？？？', url)
									} catch(e) {
										console.log(e)
										uni.navigateTo({
											url: '/' + curRoute
										})
									}
								},
								fail: () => {},
								complete: () => {
									 params.success(res.data);
								}
							});
					 
						}
					});
				}  else {
					// console.log(res, '返回的数11据？？？？/*  */')
					typeof params.success == "function" && params.success(res.data);
				}
			} else {
				 params.success(res);
			}
			

		},
		fail: err => {
			console.log('err----------',err)
			uni.showToast({
				title: err.msg || '未知错误',
				duration: 2000,
				icon: 'none'
			});
			typeof params.fail == "function" && params.fail(err.data);
		},
		complete: (e) => {
			// console.log("请求完成---------------------------------------",e);
			setTimeout(() => {
				uni.hideLoading()
			}, 3000)
			uni.stopPullDownRefresh();
			typeof params.complete == "function" && params.complete(e.data);

			return;
		}
	})
}